/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * AddNewBillingTest.java
 *
 * Created on 21-04-2012, 16:57:09
 */
package GUI;

import entities.Test;
import java.util.ArrayList;
import javax.swing.JOptionPane;
import model.Bill.TableModelTest;

/**
 *
 * @author Dell
 */
public class AddNewBillingTest extends javax.swing.JDialog {

    private ArrayList<Test> testList, testList1;
    private AddNewBill addNewBill;

    /** Creates new form AddNewBillingTest */
    private void setModelTest() {
        String[] header = {"Test Name", "Test Cost"};
        TableModelTest tmp = new TableModelTest(testList1);
        tmp.setHeader(header);
        tblTest.setModel(tmp);
    }

    public AddNewBillingTest(java.awt.Frame parent, boolean modal, ArrayList<Test> testList, AddNewBill addNewBill) {
        super(parent, modal);
        initComponents();
        setModelTest();
        this.testList = testList;
        //   this.addNewBill=new AddNewBill(parent, modal, null, null, null);
        this.addNewBill = addNewBill;
    }

    public void loadTest() {
        testList1 = new ArrayList<Test>();
        boolean flag = true;
        for (int k = 0; k < this.testList.size(); k++) {
            String name = this.testList.get(k).getTestName();
            int i = 0, j = 0, m, n;
            n = name.length() - 1;
            m = txtTestName.getText().length() - 1;
            do {
                if (name.charAt(i) == txtTestName.getText().charAt(j)) {
                    i++;
                    j++;

                } else {
                    i = i - j + 1;
                    j = 0;

                }

            } while (j <= m && i <= n);
            if (j > m) {

                //  JOptionPane.showMessageDialog(rootPane, this.testList.get(k).getTestName());

                flag = false;
                testList1.add(this.testList.get(k));


            }

        }

        if (flag) {
            JOptionPane.showMessageDialog(rootPane, "Not Exist!!!");

        }
        setModelTest();

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        txtTestName = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblTest = new javax.swing.JTable();
        btAdd = new javax.swing.JButton();
        btCancel = new javax.swing.JButton();
        btSearch = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel1.setText("Test");

        jLabel2.setText("Test Name");

        txtTestName.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtTestNameActionPerformed(evt);
            }
        });

        tblTest.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(tblTest);

        btAdd.setText("Add Test");
        btAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btAddActionPerformed(evt);
            }
        });

        btCancel.setText("Cancel");

        btSearch.setText("Search");
        btSearch.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btSearchActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addGap(152, 152, 152)
                                .addComponent(jLabel1))
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addGap(18, 18, 18)
                                .addComponent(jLabel2)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(txtTestName, javax.swing.GroupLayout.PREFERRED_SIZE, 142, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btSearch))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(73, 73, 73)
                        .addComponent(btAdd)
                        .addGap(26, 26, 26)
                        .addComponent(btCancel))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 294, Short.MAX_VALUE)))
                .addGap(16, 16, 16))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtTestName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btSearch))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btCancel)
                    .addComponent(btAdd))
                .addContainerGap(19, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void txtTestNameActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtTestNameActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtTestNameActionPerformed

    private void btSearchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btSearchActionPerformed
        loadTest();

    }//GEN-LAST:event_btSearchActionPerformed

    private void btAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btAddActionPerformed
        int i = tblTest.getSelectedRow();
        if (i >= 0) {
            addNewBill.addNewBillingTest(testList1.get(i));
            this.dispose();
            addNewBill.setVisible(true);
        }

    }//GEN-LAST:event_btAddActionPerformed
    /**
     * @param args the command line arguments
     */
//    public static void main(String args[]) {
//        java.awt.EventQueue.invokeLater(new Runnable() {
//            public void run() {
//                AddNewBillingTest dialog = new AddNewBillingTest(new javax.swing.JFrame(), true);
//                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
//                    public void windowClosing(java.awt.event.WindowEvent e) {
//                        System.exit(0);
//                    }
//                });
//                dialog.setVisible(true);
//            }
//        });
//    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btAdd;
    private javax.swing.JButton btCancel;
    private javax.swing.JButton btSearch;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tblTest;
    private javax.swing.JTextField txtTestName;
    // End of variables declaration//GEN-END:variables
}
